/**
 * @Author: oldTea
 * @CreateTime: 2024-11-25
 * @Description:
 * @Version: 1.0
 */
package com.ambition.demo.juc;

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.ReentrantLock;



/**
 * JUC包的Condition的线程阻塞与唤醒  await signal。 需配合锁使用
 *@Author: oldTea
 *@CreateTime: 2024-11-25
 *@Description:
 *@Version: 1.0
 */
public class ConditionAwait {
      static int num;
    public static void main(String[] args) {

        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        new Thread(() -> {
            try {
                System.out.println("A线程被阻塞之前");
                lock.lock();
                condition.await();
                System.out.println("A线程被B线程唤醒");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }finally {
                lock.unlock();
            }
        },"A线程").start();


        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        new Thread(() -> {
            try {
                lock.lock();
                condition.signal();
                System.out.println("B线程唤醒A线程");
            } catch (Exception e) {
                throw new RuntimeException(e);
            }finally {
                lock.unlock();
            }
        },"B线程").start();
    }
}




